def gen(x1, a1):
    j = 1
    while j < len(x1):
        for i in range(len(x1)):
            if i + j < len(x1) + 1:
                a1.append(x1[i:i + j])
        j += 1
    a1.append(x1)
    for i in range(len(a1)):
        a1[i] = int(a1[i])
    a1 = set(a1)
    a1 = sorted(a1)
    return a1


def check(a1, c1):
    b = 0
    for j in range(len(a1)):
        b = 0
        for i in range(2, a1[j]):
            if a1[j] % i == 0:
                b += 1
        if b == 0 and a[j] != 0 and a[j] != 1 and c1 < a[j]:
            c1 = a[j]
    return c1


x = int(input())
a = []
c = 0
if x < 1000 or x > 9999:
    print(0)
    exit()
x = str(x)
a = gen(x, a)
c = check(a, c)
print(c)